//
//  KeRenUserHeaderView.m
//  KEREN
//
//  Created by chenliang on 2023/8/24.
//

#import "KeRenUserHeaderView.h"
#import "KeRenUserHeaderCollectCell.h"
#import "ScanDeviceViewController.h"
#import "KRGarageViewController.h"
@interface KeRenUserHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *myColllectV;
@property (nonatomic,strong) NSArray *deviceList;
@end

@implementation KeRenUserHeaderView

-(void)awakeFromNib{
    [super awakeFromNib];
    NSLog(@"=======awakeFromNib");
    [self.myColllectV registerNib:[UINib nibWithNibName:@"KeRenUserHeaderCollectCell" bundle:nil] forCellWithReuseIdentifier:@"headerIdentifier"];
}

-(void)setDataSource:(NSArray *)dataSource{
    self.deviceList = dataSource;
    [self.myColllectV reloadData];
}

#pragma mark- UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(80, 80);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UINavigationController *navVc = (UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    if(indexPath.item <self.deviceList.count){
        KRGarageViewController *vc = [[KRGarageViewController alloc] init];
        [navVc pushViewController:vc animated:YES];
    }else{
        if(self.deviceList.count > 0){
            ScanDeviceViewController *vc = [[ScanDeviceViewController alloc] init];
            [navVc pushViewController:vc animated:YES];
        }else{
            KRGarageViewController *vc = [[KRGarageViewController alloc] init];
            [navVc pushViewController:vc animated:YES];
        }
    }
}

#pragma mark- UICollectionViewDataSource
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    KeRenUserHeaderCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath];
    cell.backView.layer.cornerRadius = 25;
    cell.backView.layer.borderWidth = 1;
    cell.bView.layer.cornerRadius = 15;
    if(indexPath.item < self.deviceList.count){
        cell.bView.hidden = NO;
        cell.imgV.layer.cornerRadius = 25;
        DeviceInfo *deviceInfo = self.deviceList[indexPath.item];
        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:deviceInfo.photo] placeholderImage:[UIImage imageNamed:@"devcie_defalaut_icon"] options:SDWebImageRefreshCached];
        if(([UserInfoManager sharedInstance].currentDeviceInfo && deviceInfo.Id == [UserInfoManager sharedInstance].currentDeviceInfo.Id)){
            cell.backView.layer.borderColor = Color_7ACC00.CGColor;
            cell.bView.backgroundColor = Whire_Color;
        }else{
            cell.backView.layer.borderColor = Color_borderColor;
            cell.bView.backgroundColor = rgba(1, 1, 1, 0.2);
        }
        cell.img_width_c.constant = 80;
    }else{
        cell.bView.hidden = YES;
        cell.imgV.layer.cornerRadius = 0;
        cell.imgV.image = [UIImage imageNamed:@"Device_add"];
        cell.img_width_c.constant = 60;
        if(self.deviceList.count == 0){
            cell.backView.layer.borderColor = Color_7ACC00.CGColor;
        }else{
            cell.backView.layer.borderColor = rgba(1, 1, 1, 0.1).CGColor;
        }
    }
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.deviceList.count + 1;
}
@end
